/*
 * Eugen Stoian <stoian.e@gmail.com> 2010
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "OpDIVNode.h"
#include "OpMULNode.h"
#include "OpSUBNode.h"
#include "OpPOWNode.h"
#include "ValueNode.h"

static Real opDIV(const Real& left,const Real& right) {
    return left / right;
}

static SmartPtr<ExpressionTreeNode> opDIVDerivativeBuilder(SmartPtr<ExpressionTreeNode> node, const QString& varName) {
    if(!node)
        return 0;
    if(node->info()!=DIV_SYMBOL)
        return 0;
    if(!node->right())
        return 0;
    if(!node->left())
        return 0;
    SmartPtr<ExpressionTreeNode> root = 0;
    SmartPtr<ExpressionTreeNode> left = 0;
    SmartPtr<ExpressionTreeNode> right = 0;
    SmartPtr<ExpressionTreeNode> dleft = 0;
    SmartPtr<ExpressionTreeNode> dright = 0;

    dleft = node->left()->derive(varName);
    if(!dleft)
        return 0;
    dright = node->right()->derive(varName);
    if(!dright){
        return 0;
    }
    left = node->left()->copy();
    right = node->right()->copy();

    root = new OpMULNode();
    root->setLeft(dleft);
    root->setRight(right);
    dleft = root;

    root = new OpMULNode();
    root->setLeft(left);
    root->setRight(dright);
    dright = root;

    root = new OpSUBNode();
    root->setLeft(dleft);
    root->setRight(dright);

    left = root;

    dleft = node->right()->copy();
    dright = new ValueNode(2.0);
    root = new OpPOWNode();
    root->setLeft(dleft);
    root->setRight(dright);

    right = root;

    root = new OpDIVNode();
    root->setLeft(left);
    root->setRight(right);

    SmartPtr<ExpressionTreeNode> tmp = root->simplified();
    return tmp;
}

OpDIVNode::OpDIVNode()
        :ArithmeticOpNode(DIV_SYMBOL, &opDIV, &opDIVDerivativeBuilder) {
}

ExpressionTreeNode* OpDIVNode::copy() const {
    SmartPtr<ExpressionTreeNode> left = 0;
    SmartPtr<ExpressionTreeNode> right = 0;
    ExpressionTreeNode *root = 0;
    if(_left)
        left = _left->copy();
    if(_right)
        right = _right->copy();
    root = new OpDIVNode();
    root->setLeft(left);
    root->setRight(right);
    return root;
}

int OpDIVNode::precedence() const {
    return DIV_PRECEDENCE;
}

SmartPtr<ExpressionTreeNode> OpDIVNode::firstLevelSimplification() const {
    SmartPtr<ExpressionTreeNode> root = 0;
    QString branchPath;
    if(!_left || !_right)
        return 0;
    SmartPtr<ExpressionTreeNode> left = _left->firstLevelSimplification();
    if(!left)
        return 0;
    SmartPtr<ExpressionTreeNode> right = _right->firstLevelSimplification();
    if(!right){
        return 0;
    }
    if(left->isValue() && right->isValue() && !left->isMathConstant() && !right->isMathConstant()){
        root = new ValueNode((*_op)(left->eval(),right->eval()));
        return root;
    }

    if(left->isOperand() && right->isOperand() &&(left->info()==right->info())){
        return new ValueNode(1.0);
    }


    if(right->isValue()){
        if(right->eval().isInvertible()){
            root = new OpMULNode();
            root->setLeft(left);
            root->setRight(new ValueNode(1.0/right->eval()));
            return root;
        }
    }
    if(left->isValue()){
        if(left->eval().isZero()){
            return new ValueNode(0.0);
        }
    }

    root = new OpDIVNode();
    root->setLeft(left);
    root->setRight(right);
    return root;
}

QString OpDIVNode::toInfixString() const {
    QString leftString, rightString;
    if(_right->isOperand()||_right->isUnaryOp())
        rightString = _right->toInfixString();
    else
        rightString = "("+_right->toInfixString()+")";
    if(_left->isOperand()||_left->isUnaryOp())
        leftString = _left->toInfixString();
    else
        leftString = "("+_left->toInfixString()+")";
    return leftString+DIV_SYMBOL+rightString;
}
